<%@ page import="lf.day0109.lottery.dao.LotteryDao" %>
<%@ page import="lf.day0109.lottery.impl.LotteryDaoImpl" %>
<%@ page import="lf.day0109.lottery.entity.User" %>
<%@ page import="lf.day0109.lottery.test.LotteryTest" %><%--
  Created by IntelliJ IDEA.
  User: 19256
  Date: 2023/1/9
  Time: 16:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>保存用户信息</title>
    <%
        //设置request请求中文乱码,
        //治本方法:找到应用服务器(tomcat/config/server.xml),添加 URIEncoding="UTF-8"
        //如下: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"   URIEncoding="UTF-8"/>
        //获取addUser.jsp页面中,用户名和手机号
        String userName = request.getParameter("userName");
        String phone = request.getParameter("phone");

        //多态体现,创建LotteryDao数据访问层
        LotteryDao lotteryDao = new LotteryDaoImpl();
        //创建User对象,用来保存添加用户信息
        User user = new User();
        //用户名
        user.setUserName(userName);
        //手机号
        user.setPhone(phone);
        //中签编码(根据要求随机生成,调用随机生成方法)
        int num = (int)(8 + Math.random() * (11 - 8 + 1));
        String str = "abcdefghijklmnopqrstuvwxyz0123456789";
        int len = str.length();
        StringBuffer markNo = new StringBuffer();
        for (int i =0;i < num;i++){
            markNo.append(str.charAt((int) Math.round(Math.random() * (len - 1))));
        }
        user.setMarkNo(markNo.toString());
        int addRes = lotteryDao.addUser(user);
        if ( addRes > 0 ) {
            // 跳转到index.jsp页面
            // 转发 和 重定向
            request.getRequestDispatcher("Lottery.jsp").forward(request,response);
        }else{
            out.print("添加失败!!!");
        }

    %>
</head>
<body>

</body>
</html>
